// functionPage/pages/userPassword/historyRecord/historyRecord.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabsArr: [{
            title: '单人口令',
            count: 0
        }, {
            title: '会议口令',
            count: 0
        }],
        currentTabIndex: 0,
        // 单人模式
        singleModeArr: [],
        // 会议模式
        meetingModeArr: [],
        params: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.type) {
            this.setData({
                currentTabIndex: options.type == '01' ? 1 : 0
            })
        }
        this.getValidPasswordCount()
    },

    onTabsClick(e) {
        this.setData({
            currentTabIndex: e.detail.index
        })
    },

    swiperChange(e) {
        this.setData({
            currentTabIndex: e.detail.current
        })
    },

    handleLoad(e) {
        let {
            type
        } = e.currentTarget.dataset;

        let instance = this.selectComponent(`.${type}`)

        let temp = []

        instance.data.responseArr.map(item => {
            item.data.map(child => {
                temp.push(child)
            })

        })

        this.setData({
            [`${type}ModeArr`]: temp
        })
    },

    // 获取口令有效数字
    getValidPasswordCount() {
        let _that = this
        wx.request({
            url: app.globalData.rootPath + `cjMap/countPasswordType`,
            data: {
                authId: app.globalData.authId
            },
            success: function(res) {
                if (res.data.status === "0") {
                    _that.data.tabsArr[0].count = res.data.data.singlepwd
                    _that.data.tabsArr[1].count = res.data.data.meetingpwd
                    _that.setData({
                        tabsArr: _that.data.tabsArr
                    })
                }
            }
        });
    },

    // 复制
    copyText: function(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.content,
            success: function(res) {
                wx.getClipboardData({
                    success: function(res) {
                        wx.showToast({
                            title: '口令复制成功',
                            icon: 'none',
                            duration: 1500
                        })
                    }
                })
            }
        })
    },

    // 关闭口令
    closeVisitorPassword(e) {
        let _that = this
        wx.showModal({
            title: '提示',
            content: '关闭后，该口令将失效，无法再次启用，确定关闭？',
            success: function(res) {
                if (res.confirm) {
                    let {
                        id,
                        type,
                        index
                    } = e.currentTarget.dataset
                    wx.request({
                        url: app.globalData.rootPath + `cjMap/closeVisitorPassword`,
                        data: {
                            authId: app.globalData.authId,
                            id,
                            delflg: '01'
                        },
                        success: function(res) {
                            if (res.data.status === "0") {
                                console.log(type)
                                if (type == '00') {
                                    _that.data.singleModeArr[index].delflg = '01'
                                    _that.setData({
                                        singleModeArr: _that.data.singleModeArr
                                    })
                                } else if (type == '01') {
                                    _that.data.meetingModeArr[index].delflg = '01'
                                    _that.setData({
                                        meetingModeArr: _that.data.meetingModeArr
                                    })

                                }
                                // 刷新数字点
                                _that.getValidPasswordCount()

                                let pages = getCurrentPages(); //当前页面栈
                                let prevPage = pages[pages.length - 2]; //上一页面
                                prevPage.getValidPasswordCount()
                            }
                        }
                    });
                } else {
                    console.log('点击取消回调')
                }
            }
        })
    },

    handleOver(e) {
        let _that = this
        let {
            id,
            type,
            index
        } = e.currentTarget.dataset
        wx.request({
            url: app.globalData.rootPath + `cjMap/closeVisitorPassword`,
            data: {
                authId: app.globalData.authId,
                id,
                delflg: '01'
            },
            success: function(res) {
                if (res.data.status === "0") {
                    console.log(type)
                    if (type == '00') {
                        _that.data.singleModeArr[index].delflg = '01'
                        _that.setData({
                            singleModeArr: _that.data.singleModeArr
                        })
                    } else if (type == '01') {
                        _that.data.meetingModeArr[index].delflg = '01'
                        _that.setData({
                            meetingModeArr: _that.data.meetingModeArr
                        })

                    }
                    // 刷新数字点
                    _that.getValidPasswordCount()

                    let pages = getCurrentPages(); //当前页面栈
                    let prevPage = pages[pages.length - 2]; //上一页面
                    prevPage.getValidPasswordCount()
                }
            }
        });
    }
})